﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SEP.Admin;
using DataAccessLogic;
using System.Configuration;

namespace BusinessLogic
{
    [Serializable()]
    public class ServiceReport : Table
    {
        #region Fields
        private ServiceTotalList _totals;
        private PartList _parts;
        private Instrument _instrument;
        private string _serviceRequired;
        private string _servicePerformed;
        private string _poNumber;
        private Guid _jobID;
        private bool _archived;
        private string _templatePath;
        private bool _sentToIan;
        private bool _sentToJenny;
        #endregion

        #region Methods
        public bool Create()
        {
            bool result = false;

            this.ID = ServiceReportDataAccess.Create(this);
            
            if(Guid.Empty != this.ID)
            {
                result = true;
            }

            return result;
        }
                
        public static List<ServiceReport> GetAllSummariesByJobID(Guid jobID)
        {
            return ServiceReportDataAccess.GetAllSummariesByJobID(jobID);
        }
        
        public static ServiceReport GetByID(Guid serviceReportID)
        {
            return ServiceReportDataAccess.GetByID(serviceReportID);   
        }

        public static byte[] CreateExcel(ServiceReport report)
        {
            Job j = new Job();            
            j = j.GetByID(report.JobID);

            if(!string.IsNullOrWhiteSpace(report.TemplatePath))
            {
                ExcelCreatorData data = new ExcelCreatorData();
                data.TemplatePath = report.TemplatePath;
                data.FileName = report.DateAdded.ToShortDateString();

                if(j.Location != null)
                {
                    data.Data.Add(new ExcelCreatorDataObject("C6", eExcelCreatorDataObjectType.CELL), j.Location.Name);

                    if(j.Location.Address != null)
                    {
                        data.Data.Add(new ExcelCreatorDataObject("C7", eExcelCreatorDataObjectType.CELL), j.Location.Address.AddressBlock);
                        data.Data.Add(new ExcelCreatorDataObject("C8", eExcelCreatorDataObjectType.CELL), j.Location.Address.City);
                        data.Data.Add(new ExcelCreatorDataObject("E8", eExcelCreatorDataObjectType.CELL), j.Location.Address.PostCode);

                        if(j.Location.Address.State != null)
                        {
                            data.Data.Add(new ExcelCreatorDataObject("D8", eExcelCreatorDataObjectType.CELL), j.Location.Address.State.Abbreviation);
                        }
                    }

                    if(j.Location.Phone != null)
                    {
                        data.Data.Add(new ExcelCreatorDataObject("C10", eExcelCreatorDataObjectType.CELL), j.Location.Phone.Number);
                    }
                }

                if(j.BillTo != null)
                {
                    data.Data.Add(new ExcelCreatorDataObject("H6", eExcelCreatorDataObjectType.CELL), j.BillTo.Name);

                    if (j.BillTo.Address != null)
                    {
                        data.Data.Add(new ExcelCreatorDataObject("H7", eExcelCreatorDataObjectType.CELL), j.BillTo.Address.AddressBlock);
                        data.Data.Add(new ExcelCreatorDataObject("H8", eExcelCreatorDataObjectType.CELL), j.BillTo.Address.City);
                        data.Data.Add(new ExcelCreatorDataObject("L8", eExcelCreatorDataObjectType.CELL), j.BillTo.Address.PostCode);

                        if (j.BillTo.Address.State != null)
                        {
                            data.Data.Add(new ExcelCreatorDataObject("J8", eExcelCreatorDataObjectType.CELL), j.BillTo.Address.State.Abbreviation);
                        }
                    }

                    if (j.BillTo.Phone != null)
                    {
                        data.Data.Add(new ExcelCreatorDataObject("H10", eExcelCreatorDataObjectType.CELL), j.BillTo.Phone.Number);
                    }
                }

                //contact C9                
                //fax C11
                //email C12

                //contact H9                
                //fax H11
                //email H12
                return ExcelCreator.Create(data);
            }
            else
            {
                throw new ArgumentNullException("TemplatePath", "Please specify a template path for your service report.");
            }
        }

        public static void Delete(Guid serviceReportID)
        {
            ServiceReportDataAccess.Delete(serviceReportID);
        }
        public static void SendToIan(Guid serviceReportID)
        {
            ServiceReportDataAccess.SendToIan(serviceReportID);

            string to = ConfigurationManager.AppSettings["Ian"].ToString();
            SendToEmail(to, serviceReportID);
        }

        public static void SendToJenny(Guid serviceReportID)
        {
            ServiceReportDataAccess.SendToJenny(serviceReportID);

            string to = ConfigurationManager.AppSettings["Jenny"].ToString();
            SendToEmail(to, serviceReportID);
        }

        private static string SendToEmail(string recipient, Guid serviceReportID)
        {
            string result = string.Empty;

            try
            {
                string body = string.Format(SEPDefaults.Other.EmailServiceReportSentToBody, serviceReportID.ToString());
                Common.Email.Send(recipient, "no-reply@southeastpathology.com", SEPDefaults.Other.EmailSentToSubject, body, true);
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return result;
        }
        #endregion

        #region Properties
        public bool Archived
        {
            get 
            {
                return _archived;
            }
            set
            {
                _archived = value;
            }
        }

        public ServiceTotalList Totals
        {
            get 
            {
                return _totals;
            }
            set
            {
                _totals = value;    
            }
        }

        public PartList Parts
        {
            get
            {
                return _parts;
            }
            set
            {
                _parts = value;
            }
        }
        
        public Instrument Instrument
        {
            get
            {
                return _instrument;
            }
            set
            {
                _instrument = value;
            }
        }

        
        public string ServiceRequired
        {
            get
            {
                return _serviceRequired;
            }
            set
            {
                _serviceRequired = value;
            }
        }       
        
        public string ServicePerformed
        {
            get
            {
                return _servicePerformed;
            }
            set
            {
                _servicePerformed= value;
            }
        }
                
        public string PONumber
        {
            get
            {
                return _poNumber;
            }
            set
            {
                _poNumber = value;  
            }
        }

        public Guid JobID
        {
            get 
            {
                return _jobID;
            }
            set
            {
                _jobID = value;
            }
        }

        public string TemplatePath
        {
            get 
            {
                return _templatePath;
            }
            set
            {
                _templatePath = value;
            }
        }

        public bool SentToIan
        {
            get
            {
                return _sentToIan;
            }
            set
            {
                _sentToIan = value;
            }
        }

        public bool SentToJenny
        {
            get
            {
                return _sentToJenny;
            }
            set
            {
                _sentToJenny = value;
            }
        }
        #endregion
    }
}